Handling Pointers in Program Code in a System that Supports Multiple Address Spaces

ABSTRACT

Some embodiments include a processing subsystem that compiles program code to generate compiled program code. In these embodiments, while compiling the program code, the processing subsystem first identifies a pointer in the program code that points to an unspecified address space. The processing subsystem then analyzes at least a portion of the program code to determine one or more address spaces to which the pointer may point. Next, the processor updates metadata for the pointer to indicate the one or more address spaces to which the pointer may point, the metadata enabling a determination of an address space to which the pointer points during subsequent execution of the compiled program code.

RELATED APPLICATIONS

The instant application is a divisional of, and hereby claims priorityto, pending U.S. patent application Ser. No. 13/632,902, which was filedon 1 Oct. 2012. The instant application further claims priority tonow-expired U.S. provisional patent application No. 61/681,245, whichwas filed on 9 Aug. 2012, to which parent application Ser. No.13/632,902 also claims priority. Both of these applications areincorporated by reference.

This application is related to pending U.S. patent application Ser. No.13/296,967, titled “Computer System and Method for Compiling ProgramCode and Assigning Address Spaces,” by the same inventor, which wasfiled on 15 Nov. 2011.

BACKGROUND

1. Field

The described embodiments relate to techniques for handling pointers inprogram code. More specifically, the described embodiments relate to atechnique for handling pointers in program code in a system thatsupports multiple address spaces.

2. Related Art

Through the use of certain programming languages (e.g., OpenCL, OpenCLC++, CUDA, etc.), programmers are able to use secondary processors(e.g., graphics processing units (GPUs), additional central processingunit (CPUs), embedded processors, etc. for performing operations thathistorically have been performed by a CPU alone. For example, some ofthese programming languages enable a GPU to be used to offload certaintypes of operations from a CPU, thereby freeing the CPU to perform otheroperations.

Some of these programming languages include support for multipledisjoint address spaces that can be used to control accesses toexplicitly-managed memories in the secondary processors that arefurnished with such memories. For example, the OpenCL programminglanguage includes separate global, local, constant, and private addressspaces. Although these address spaces are useful for handling memoryaccesses, their use introduces significant limitations to the underlyingprogramming languages. One such limitation is that pointers withinprogram code must be expressly directed to a particular address space.Thus, for each pointer, a programmer is required to state an addressspace to which the pointer is directed.

The requirement that pointers be handled in this way means thatcomposing program code in these programming languages can be difficult.For example, if different versions of a function are to be implementedwith pointers to different address spaces, multiple versions of the samefunction would need to be implemented. As another example, themanagement of implicit pointers, such as a class's this pointer (in theprogramming languages that support the this pointer), can be difficultwhen the implicit pointer can be applied to multiple address spaces.

Further complicating this problem is the fact that some of theseprogramming languages support a “generic” address space that is used torefer generally to an underlying set of specific address spaces. In suchprogramming languages, upon encountering a pointer to the genericaddress space at runtime, the system is required to dynamicallydetermine an actual address space for the pointer before proceeding withsubsequent operations.

SUMMARY

Some embodiments include a compiler (e.g., compiler 302 in FIG. 3) thatcompiles program code to generate compiled program code. In theseembodiments, while compiling the program code, the compiler firstidentifies a pointer in the program code that points to an unspecifiedaddress space. The compiler then analyzes at least a portion of theprogram code to determine one or more address spaces to which thepointer may point. Next, the compiler updates metadata for the pointerto indicate the one or more address spaces to which the pointer maypoint, the metadata enabling a determination of an address space towhich the pointer points during subsequent execution of the compiledprogram code.

In some embodiments, the compiler adds one or more instructions to thecompiled program code. During subsequent execution of the compiledprogram code, the instructions are used to determine an address space towhich the pointer points based on the metadata and one or more runtimeconditions.

In some embodiments, the compiler adds one or more additionalinstructions to the compiled program code. During subsequent executionof the compiled program code, the additional instructions use thepointer to access the determined address space to which the pointerpoints. In some embodiments, the additional instructions comprise: (1) amemory access instruction configured to access each address space towhich the pointer may point (e.g., a memory access instruction for aprivate address space, a memory access instruction for a global addressspace, etc.), and (2) one or more conditional instructions that controlwhich of the memory access instructions is used to access the determinedaddress space using the pointer.

In some embodiments, when identifying the pointer in the program codethat points to the unspecified address space, the compiler first findsthe pointer in the program code. The compiler then attempts to resolvean address space to which the pointer points using information in thepointer. When an address space cannot be resolved for the pointer, thecompiler identifies the pointer as pointing to an unspecified addressspace.

In some embodiments, identifying the pointer in the program code thatpoints to the unspecified address space comprises at least one of: (1)identifying the pointer as pointing to a generic address space; or (2)identifying different instances of the pointer in the program code aspointing to two or more different address spaces.

In some embodiments, when analyzing at least the portion of the programcode to determine one or more address spaces to which the pointer maypoint, the compiler can analyze the program code to determine one ormore operations in which the pointer is used that indicate an addressspace to which the pointer may point. The compiler can additionally, oralternatively, analyze operations related to the operations in which thepointer is used that indicate an address space to which the pointer maypoint, the related operations not directly using the pointer.

In some embodiments, when updating metadata for the pointer to indicatethe one or more address spaces to which the pointer may point, thecompiler can update one or more values in the pointer to indicate theone or more address spaces and/or update metadata associated with thepointer, but separate from the pointer, to indicate the one or moreaddress spaces.

Some embodiments include a secondary processing subsystem that handlespointers while executing program code. In these embodiments, whileexecuting program code, the secondary processing subsystem firstencounters a pointer in the program code that points to an unspecifiedaddress space, wherein the pointer includes metadata that indicates oneor more address spaces to which the pointer may point. The secondaryprocessing subsystem then determines an address space to which thepointer points based on the metadata and one or more runtime conditions.

In some embodiments, the secondary processing subsystem performs one ormore subsequent operations using the pointer based on the determinedaddress space. For example, the secondary processing subsystem canreceive a set of possible memory accesses that comprises a separatememory access for each of the one or more address spaces to which thepointer may point. The secondary processing subsystem can then determinea memory access to be performed using the pointer from the set ofpossible memory accesses based on the determined address space. Next,the secondary processing subsystem can perform the determined memoryaccess using the pointer.

In some embodiments, when determining the address space to which thepointer points based on the metadata and the one or more runtimeconditions, the secondary processing subsystem collects informationabout the one or more runtime conditions, the information indicating atleast one candidate address space to which the pointer may point. Thesecondary processing subsystem then uses the collected information andthe metadata to determine an address space to which the pointer points.

In some embodiments, collecting the information about the one or moreruntime conditions comprises collecting information from one of: (1)operations that use the pointer, and (2) operations related tooperations that use the pointer that do not directly use the pointer.

BRIEF DESCRIPTION OF THE FIGURES

FIG. 1 presents a block diagram illustrating an electronic device inaccordance with some embodiments.

FIG. 2 presents a block diagram illustrating a secondary processingsubsystem in accordance with some embodiments.

FIG. 3 presents a block diagram illustrating a computer-readable storagemedium in accordance with some embodiments.

FIG. 4 presents a block diagram illustrating address spaces inaccordance with some embodiments.

FIG. 5 presents a flowchart illustrating a process for compiling programcode to generate compiled program code in accordance with someembodiments.

FIGS. 6A-6B present examples of metadata for pointers in accordance withsome embodiments.

FIG. 7 presents a flowchart illustrating handling a pointer whileexecuting program code in accordance with some embodiments.

Throughout the figures and the description, like reference numeralsrefer to the same figure elements.

DETAILED DESCRIPTION

The following description is presented to enable any person skilled inthe art to make and use the described embodiments, and is provided inthe context of a particular application and its requirements. Variousmodifications to the described embodiments will be readily apparent tothose skilled in the art, and the general principles defined herein maybe applied to other embodiments and applications without departing fromthe spirit and scope of the described embodiments. Thus, the describedembodiments are not limited to the embodiments shown, but are to beaccorded the widest scope consistent with the principles and featuresdisclosed herein.

In some embodiments, an electronic device with computing capabilities(e.g., electronic device 100 in FIG. 1) can use code and/or data storedon a computer-readable storage medium to perform some or all of theoperations herein described. More specifically, the electronic devicecan read the code and/or data from the computer-readable storage mediumand can execute the code and/or use the data when performing thedescribed operations. For example, processing subsystem 102 can readprogram code for compiler 302 (see FIG. 3) from a computer-readablestorage medium and can execute the program code for compiler 302 toperform a compilation operation to generate compiled program code 306from program code 304. As another example, secondary processingsubsystem 106 can read compiled program code 306 from acomputer-readable storage medium and can execute compiled program code306 to perform the operations of compiled program code 306.

A computer-readable storage medium can be any device or medium orcombination thereof that can store code and/or data for use by anelectronic device with computing capabilities. For example, thecomputer-readable storage medium can include, but is not limited to,volatile memory or non-volatile memory, including flash memory, randomaccess memory (RAM, SRAM, DRAM, RDRAM, DDR/DDR2/DDR3 SDRAM, etc.),read-only memory (ROM), and/or magnetic or optical storage mediums(e.g., disk drives, magnetic tape, CDs, DVDs). In the describedembodiments, the computer-readable storage medium does not includenon-statutory computer-readable storage mediums such as transitorysignals.

In some embodiments, one or more hardware modules are configured toperform the operations herein described. For example, the hardwaremodules can comprise, but are not limited to, one or moreprocessors/processor cores, application-specific integrated circuit(ASIC) chips, field-programmable gate arrays (FPGAs), embeddedprocessors, graphics processors/cores, pipelines, and/or otherprogrammable-logic devices. When such hardware modules are activated,the hardware modules can perform some or all of some operations. In someembodiments, the hardware modules include one or more general-purposecircuits that are configured by executing instructions (program code,firmware, etc.) to perform the operations.

Overview

Some embodiments include a compiler for compiling program code writtenin a programming language that supports using multiple disjoint addressspaces to control accesses to memory. In these embodiments, as part of acompilation operation, the compiler can add information to compiledprogram code that can be used to determine an address space to which apointer points. More specifically, while compiling program code, thecompiler identifies a pointer in the program code for which an addressspace cannot be completely resolved at compile time (which can bereferred to herein as a pointer to an “unspecified” address space). Thecompiler can then perform a syntactic analysis of at least some of theprogram code to determine one or more address spaces to which thepointer may point. Upon determining the address spaces to which thepointer may point, the compiler can update metadata associated with thepointer in the compiled program code to indicate the address spaces towhich the pointer may point. In some embodiments, the compiler addsinstructions to the compiled program code to enable the determination ofthe address space to which a given instance of the pointer points basedon runtime conditions (and the metadata), and can add one or moreadditional instructions to the compiled program code for performingother operations based on the determined address space.

When the compiled program code is subsequently executed, e.g., by asecondary processing subsystem, the secondary processing subsystem canexecute the instructions in the compiled program code to determine theaddress space to which a given instance of the pointer points based onruntime conditions and the metadata, and can execute the additionalinstructions for performing other operations based on the determinedaddress space.

Electronic Device

FIG. 1 presents a block diagram illustrating an electronic device 100 inaccordance with some embodiments. Generally, electronic device 100 canbe any electronic device with computing capabilities that can performthe operations herein described. As can be seen in FIG. 1, electronicdevice 100 includes processing subsystem 102, memory subsystem 104, andsecondary processing subsystem 106.

Processing subsystem 102 includes one or more devices, circuits,hardware modules, and/or computer-readable storage mediums configuredfor performing computational operations. For example, processingsubsystem 102 can include, but is not limited to, one or moreprocessors/processor cores, ASICs, microcontrollers, orprogrammable-logic devices.

Memory subsystem 104 includes one or more devices, circuits, hardwaremodules, and/or computer-readable storage mediums for storing codeand/or data for use by processing subsystem 102 and/or other subsystemsin electronic device 100, as well as for controlling access to the codeand/or data. For example, memory subsystem 104 can include, but is notlimited to, DRAM, flash memory, and/or other types of memory.

In some embodiments, memory subsystem 104 includes some or all of amemory hierarchy that comprises an arrangement of one or more cachescoupled to a memory in electronic device 100. In these embodiments, oneor more of the caches in the memory hierarchy can be located inprocessing subsystem 102. In addition, in some embodiments, memorysubsystem 104 is coupled to one or more high-capacity mass-storagedevices (not shown). For example, memory subsystem 104 can be coupled toa magnetic or optical drive, a solid-state drive, or another type ofmass-storage device. In these embodiments, memory subsystem 104 canstore more recently and/or frequently accessed code and/or data, whilethe mass-storage device stores less recently and/or less frequentlyaccessed code and/or data.

Secondary processing subsystem 106 includes one or more devices,circuits, hardware modules, and/or computer-readable storage mediumsconfigured for performing computational operations. For example,secondary processing subsystem 106 can be a graphics processing unit(GPU), a general-purpose GPU (GPGPU), a processor/processor core (e.g.,in a CPU), and/or another type of embedded processor that includes oneor more processors/processor cores, pipelines, ASICs, microcontrollers,and/or programmable-logic devices.

As can be seen in FIG. 2, which presents an expanded view of someembodiments of secondary processing subsystem 106, secondary processingsubsystem 106 comprises processing subsystem 200 and memory hierarchy202. Processing subsystem 200 comprises one or more processingmechanisms for performing computational operations (e.g., processorcores, pipelines, etc.). Memory hierarchy 202 comprises an arrangementof one or more separate memory circuits (e.g., semiconductor memories,etc.) that store instructions and/or data for processing subsystem 200,and that can be explicitly managed by processing subsystem 200 and/orother mechanisms in secondary processing subsystem 106 (not shown).

In some embodiments, secondary processing subsystem 106 is a GPU/GPGPUthat performs computational operations related to the rendering ofgraphical data for display by a display subsystem (not shown). In someembodiments, secondary processing subsystem 106 is a computationaldevice that performs one or more other operations in the system (e.g.,an additional processor/processor core (e.g., in a CPU) a mediaprocessor, a system maintenance processor, a network processor, acontroller, an embedded processor, etc.).

Although secondary processing subsystem 106 is referred to as“secondary” in this description, this language is simply to clarify thisdescription and does not necessarily limit the functions performed bysecondary processing subsystem 106 with respect to processing subsystem102 and/or any other subsystem in electronic device 100. For example, insome embodiments, secondary processing subsystem 106 comprises one ormore CPU/cores/pipelines that can be configured (e.g., by executingprogram code) to perform operations similar to those performed byprocessing subsystem 102. Generally, secondary processing subsystem 106can be any type of processing subsystem that provides one or moremechanisms for using the address spaces herein described for handlingaccesses to memory.

In some embodiments, secondary processing subsystem 106 can performcomputational operations offloaded from processing subsystem 102,thereby freeing processing subsystem 102 to perform other computationaloperations. In these embodiments, secondary processing subsystem 106 canbe programmed using programming code in an appropriate programminglanguage (e.g., OpenCL, CUDA, etc.) to perform these operations forprocessing subsystem 102. For example, in some embodiments, secondaryprocessing subsystem 106 can execute compiled program code 306, whichcauses secondary processing subsystem 106 to perform operations fromprocessing subsystem 102, as is explained in more detail below.

Bus 108 is coupled between processing subsystem 102 and memory subsystem104 and bus 108 is coupled between processing subsystem 102 andsecondary processing subsystem 106. Busses 108 and 110 can beelectrical, optical, and/or electro-optical connections used tocommunicate between processing subsystem 102 and memory subsystem 104and secondary processing subsystem 106, respectively.

Although shown as separate subsystems in FIG. 1, in some embodiments,some or all of a given subsystem can be integrated into one or more ofthe other subsystems in electronic device 100. For example, as describedabove, some or all of memory subsystem 104 can be included in processingsubsystem 102. In addition, or alternatively, some or all of secondaryprocessing subsystem 106 can be included in processing subsystem 102.

Electronic device 100 can be, or can be incorporated into, manydifferent types of electronic devices. Generally, these electronicdevices include any device that can perform the operations hereindescribed. For example, electronic device 100 can be part of a desktopcomputer, a laptop computer, a server, a media player, an appliance, asubnotebook/netbook, a tablet computer, a smart phone, a networkappliance, a set-top box, a toy, a controller, or another device.

Although specific components are used to describe electronic device 100,in some embodiments, different components and/or subsystems may bepresent in electronic device 100. For example, electronic device 100 mayinclude one or more additional processing subsystems 102, memorysubsystems 104, and/or secondary processing subsystems 106.Alternatively, one or more of the subsystems may not be present inelectronic device 100. Moreover, in some embodiments, electronic device100 may include one or more additional subsystems that are not shown inFIG. 1. For example, electronic device 100 can include, but is notlimited to, a display subsystem, a networking subsystem, a datacollection subsystem, an audio subsystem, and/or an input/output (I/O)subsystem.

Compiler

FIG. 3 presents a block diagram illustrating a computer-readable storagemedium 300 (e.g., which can be included in memory subsystem 104) thatcomprises a compiler 302, program code 304, and compiled program code306 in accordance with some embodiments. During operation, processingsubsystem 102 can execute compiler 302, which compiles program code 304into compiled program code 306. In compiled program code 306, one ormore pointers can comprise metadata that can be used to determine anaddress space to which the pointers point. Compiled program code 306 canthen be executed by secondary processing subsystem 106.

In some embodiments, program code 304 is written in a programminglanguage that can be compiled into compiled program code 306 that is tobe executed by a secondary processing subsystem 106 (e.g., OpenCL,OpenCL C++, CUDA, etc.). In these embodiments, program code 304 (andhence compiled program code 306) can comprise one or more instructionsthat cause secondary processing subsystem 106 to offload one or morecomputational operations from processing subsystem 102. In other words,compiled program code 306, when executed by secondary processingsubsystem 106, can cause secondary processing subsystem 106 to undertakeone or more computational operations on behalf of processing subsystem102 to lessen the computational load on processing subsystem 102. Forexample, in some embodiments, secondary processing subsystem 106includes circuits/processors that are particularly suited for performingsimultaneous instruction multiple data (SIMD) and/or vector operations.In these embodiments, upon executing compiled program code 306,secondary processing subsystem 106 can be configured to accept SIMDand/or vector operations from processing subsystem 102, perform theoperations, and return the results to processing subsystem 102, therebyavoiding the need for processing subsystem 102 to perform theseoperations itself.

Although computer-readable storage medium 300 is described as anexample, in some embodiments, some or all of compiler 302, program code304, and/or compiled program code 306 can be stored in separatecomputer-readable storage mediums. For example, in some embodiments, acopy of program code 304 is stored in memory subsystem 104, and a copyof compiled program code 306 is stored in memory hierarchy 202. Asanother example, program code 304 can be stored in a memory subsystem ina programmer's electronic device 100 (e.g., desktop computer, server,laptop, etc.), and copies of compiled program code 306 can besold/distributed to users to be stored in a computer-readable storagemedium on the user's electronic device 100 (e.g., desktop computer,laptop computer, smart phone, etc.).

Aside from the operations herein described, compiler 302 performsoperations known in the art for compiling programming code to generatecompiled program code.

Address Spaces

Some embodiments support programming languages that use multipledisjoint address spaces (e.g., OpenCL, CUDA, etc.) to control accessesto explicitly-managed memory hierarchy 202 in secondary processingsubsystem 106. For example, in some embodiments, these address spacescomprise a global address space, a local address space, a constantaddress space, and a private address space. FIG. 4 presents a blockdiagram of global address space 402, constant address space 404, localaddress space 406, and private address spaces (“PAS”) 408 in each ofwork items 410-414 in kernel 400 in accordance with some embodiments.Generally, each of the address spaces 402-408 includes a group of memoryaddresses in memory hierarchy 202 that is separate and does not overlapthe other address spaces, i.e., is disjoint.

The address spaces in FIG. 4 are associated with, and accessible to,various work items in kernel 400 (which can be executed by secondaryprocessing subsystem 106) in accordance with a scope of the addressspace. For example, global address space 402 and constant address space404 include addresses for portions of memory hierarchy 202 that are(globally) visible to all work groups 416-420 (and, hence, to the workitems in each work group, such as work items 410-414 in work group 416).In addition, local address space 406 includes a portion of memoryhierarchy 202 that is visible to all work items 410-414 in the workgroup 416, and private address space 408 includes a portion of memoryhierarchy 202 that is visible to the corresponding work item 410-414.

Some embodiments also support programming languages that use a genericaddress space. Generally, the generic address space is used for pointersfor which secondary processing subsystem 106 determines an address'disjoint address space at runtime (or “dynamically”) as compiled programcode is executed. Generic address spaces enable dynamic casting betweengeneric and non-generic address spaces that is similar to the dynamicsubtyping found in objected oriented programming languages.

Note that, although FIG. 4 presents example address spaces, someembodiments are not limited to these address spaces. Generally, someembodiments can update metadata for pointers in compiled program code306 to enable the resolution of address spaces for various arrangementsand types of address spaces.

Compilation

As briefly described above, as compiled program code 306 is generatedduring a compilation operation, a compiler 302 in some embodimentsupdates metadata associated with pointers with address spaceinformation. The metadata can then be used when compiled program code306 is subsequently executed (e.g., at runtime) to enable adetermination of an address space to which the pointer points (e.g., theone of address spaces 402-408 to which the pointer points).

In some embodiments, the update of the metadata for pointers can occurat any stage of a compilation process during which the requisiteinformation about program structure (address spaces, pointers, etc.) isavailable to compiler 302. For example, in some embodiments, the updateof metadata occurs in a compiler front-end, along with syntacticanalysis and generation of intermediate code, or in the compilerback-end, along with other optimization and/or code generationoperations performed on the intermediate code generated in the compilerfront-end.

FIG. 5 presents a flowchart illustrating a process for compiling programcode 304 to generate compiled program code 306 in accordance with someembodiments. Note that the operations in FIG. 5 are presented as ageneral example of some functions that may be performed by the describedembodiments. The operations performed by some embodiments includedifferent operations and/or operations that are performed in a differentorder.

The process shown in FIG. 5 starts when, during a compilation operation,compiler 302 (which can be executed by, e.g., processing subsystem 102)identifies a pointer in program code 304 that points to an unspecifiedaddress space (step 500). More specifically, during the identificationoperation, compiler 302 can first find the pointer in program code 304.For example, compiler 302 can search through program code 304 (or anintermediate representation of program code 304 generated during thecompilation process) to find pointers. Compiler 302 can then attempt toresolve an address space to which the pointer points using informationin the pointer. For each pointer whose address space can be resolved inthis way, compiler 302 can halt the processing of the pointer, leavingthe pointer without metadata (the metadata being unnecessary when theaddress space can be resolved). However, when an address space cannot beresolved for the pointer at compile-time, compiler 302 can identify thepointer as being a pointer that points to the unspecified address space.For example, compiler 302 can identify a pointer as pointing to thegeneric address space and/or can identify different instances of apointer in program code 304 as pointing to two or more different addressspaces, such as in the following example:

kernel void bar (global int *g, local int *1) {  generic int * tmp;  if(get_global_id (0) % 2)  {   tmp = g;  }  else  {   tmp = 1;  } }As can be seen in this example, the address space for tmp is generic,and different instances of tmp are set equal to a pointer from a local(l) and a global (g) address space, respectively. Compiler 302 cantherefore determine that tmp is a pointer to an unspecified addressspace because tmp: (1) is a pointer to a generic address space, and (b)different instances of tmp can point to either the global or the localaddress space.

Compiler 302 next analyzes at least a portion of program code 304 todetermine one or more address spaces to which the pointer may point(step 502). For example, compiler 302 can analyze program code 304 todetermine one or more operations in which the pointer is (directly) usedthat indicate an address space to which the pointer may point. Using theexample from above, compiler 302 may determine that different instancesof the pointer tmp are set equal to a pointer from both a local (l) anda global (g) address space. As another example, compiler 302 can analyzeoperations that do not directly use the pointer, but that are related tothe operations in which the pointer is used that indicate an addressspace to which the pointer may point, as the following exampleillustrates:

kernel void bar (global int *g, local int *1) {  generic int * tmp; generic int * zip;  if (get_global_id (0) % 2)  {   zip = g;  }  else {   zip = 1;  }  tmp = zip; }In this example, compiler 302 can determine that different instances ofzip are set equal to a pointer from a local (l) and a global (g) addressspace, respectively, and that tmp is set equal to zip. Compiler 302 canthen determine that tmp may point to either the global or the localaddress space.

Compiler 302 then updates metadata for the pointer to indicate the oneor more address spaces to which the pointer may point (step 504).Generally, the metadata can include any information that enables adetermination of an address space to which the pointer points duringsubsequent execution of compiled program code 306. In some embodiments,the metadata (which may be called a “predicate”) is contained in thepointer itself, is stored in a table in compiled program code 306,and/or is otherwise located in program code (e.g., as an annotation nearthe pointer in compiled code, etc.) or a data structure (e.g., aseparate file) associated with compiled program code 306.

FIGS. 6A-6B present examples of metadata for pointers in accordance withsome embodiments. Note that, although the embodiments shown in FIG.6A-6B can be used separately, in some embodiments, some combination ofFIGS. 6A-6B can be used.

As shown in FIG. 6A, in some embodiments, compiled program code 306includes a metadata table 600 that stores metadata for at least some ofthe pointers in compiled program code 306. As described above, theinformation in metadata table 600 can include any type of informationthat can be used by secondary processing subsystem 106 for determiningthe address spaces to which a pointer may point when executing compiledprogram code 306. For example, the information can include a valuerepresenting each address space to which the pointer may point, a valuerepresenting a possibility (e.g., a percentage) that the pointer pointsto each address space to which the pointer may point, an identifier forthe pointer and/or the instance of the pointer (e.g., a program counter,a pointer name, a program scope indication, etc.), and/or otherinformation.

As shown in FIG. 6B, in some embodiments, compiled program code 306includes a pointer 610 (along with other compiled program code). Pointer610 includes a set of address bits 612, a portion of which (e.g., agiven N bits of a M-bit pointer address, where N<M) can be used to storemetadata 614 that indicates the one or more address spaces to which thepointer may point. In these embodiments, secondary processing subsystem106 may be configured not to use all of address bits 612 in pointer 612to point to locations in memory hierarchy 202 (i.e., may operate usingpointers that are shorter than the maximum pointer length supported bysecondary processing subsystem 106). Thus, these embodiments may use asubset of N (e.g., 2, 4, etc.) bits from the M (e.g., 64, 32, etc.) bitsin pointer 610 to store metadata 614 (i.e., unused address bits may berepurposed for storing the metadata for the pointer). In theseembodiments, metadata 614 may therefore fit within an existing pointer.

Returning to FIG. 5, compiler 302 can then add one or more instructionsto compiled program code 306 for determining an address space to whichthe pointer points during subsequent execution of compiled program code306 based on the metadata and one or more runtime conditions (step 506).As one example, compiler 302 can add one or more instructions thatanalyze at least a portion of compiled program code 306 and/or runtimestate of secondary processing subsystem 106, processing subsystem 102,and/or electronic device 100 to determine an address space to which agiven instance of the pointer is directed. This can include instructionsthat check one or more runtime pointer/variable and/or register/memoryvalues, compute one or more conditional results based on runtime values(e.g., comparisons, logical operations, etc.), analyze a runtime flow ofthe program, compare one or more portions of runtime state with themetadata for the instance of the pointer, and/or perform otheroperations to determine an address space to which a pointer is directed.

In some embodiments, compiler 302 can add one or more indicatorvariables to compiled program code 306 to enable the determination of anaddress space to which a pointer is directed using the above-describedinstructions. Each indicator variable can be used to store arepresentation (e.g., a numerical value, a string, a vector, etc.) ofsome portion of a dynamic/runtime state of compiled program code 306and/or secondary processing subsystem 106, processing subsystem 102,and/or electronic device 100 to enable the analysis of a given instanceof a pointer's address space. In these embodiments, compiler 302 mayalso add instructions to corresponding locations of compiled programcode 306 to set or update the value of the indicator variables based onone or more of the program flow (e.g., which branch was taken in aconditional, whether one or more conditions have occurred, etc.), theruntime state of the system (e.g., has a register been written to,etc.), and/or other operations that indicate an address space to whichthe corresponding instance of the pointer is directed.

Next, compiler 302 can add one or more additional instructions tocompiled program code 306, the additional instructions using the pointerto access the determined address space to which the pointer points (step508). Generally, these instructions can comprise any instruction orcombination of instructions that enable secondary processing subsystem106 to perform some operation using the pointer and the determinedaddress space. In some embodiments, these instructions comprise: (1) amemory access instruction configured to access each address space towhich the pointer may point, and (2) one or more conditionalinstructions that control which of the memory access instructions isused to access the determined address space using the pointer. Forexample, assuming that the address spaces to which the pointer may pointinclude at least a global address space and a local address space, theprogramming language in which program code 304 is written can includeseparate instructions for performing memory accesses (reads and writes)for each of the address spaces, such as a “read global address space”instruction and a “read local address space” instruction. In theseembodiments, the one or more conditional instructions determine which ofthe memory access instructions is to be used based on the outcome of theaddress space determination for the corresponding instance of thepointer (i.e., the determination performed by the instructions from step506).

Program Code Execution

FIG. 7 presents a flowchart illustrating handling a pointer whileexecuting program code in accordance with some embodiments. Note thatthe operations in FIG. 7 are presented as a general example of somefunctions that may be performed by the described embodiments. Theoperations performed by some embodiments include different operationsand/or operations that are performed in a different order.

As shown in FIG. 7, the operations start when, while executing compiledprogram code 306 (e.g., program code compiled during a compilationoperation in which the operations shown in FIG. 5 are a portion),secondary processing subsystem 106 encounters a pointer in compiledprogram code 306 that points to an unspecified address space, thepointer including metadata that indicates one or more address spaces towhich the pointer may point (step 700). Recall that the metadata may beincluded in the pointer itself, in a metadata table 600 included incompiled program code 306, and/or otherwise located in program code(e.g., as an annotation near the pointer in compiled code, etc.) or adata structure (e.g., a separate file) associated with compiled programcode 306.

Secondary processing subsystem 106 then determines an address space towhich the pointer points based on the metadata and one or more runtimeconditions (step 702). When determining the address space to which thepointer points based on the metadata and the one or more runtimeconditions, secondary processing subsystem 106 can first collectinformation about the one or more runtime conditions, the informationindicating at least one candidate address space to which the pointer maypoint. Performing the collection operation can comprise executing one ormore of the instructions added to compiled program code 306 in step 506of FIG. 5 (possibly along with using indicator variables) to determinethe candidate address space (where a candidate address space is anaddress space to which the pointer could point). Secondary processingsubsystem 106 then uses the collected information and the metadata todetermine an address space to which the pointer points. For example,secondary processing subsystem 106 could perform one or more comparisonoperations, logical operations, and/or other operations to determine ifthe candidate address space is an address space to which the pointer wasdetermined (in step 502) to point. Performing the determinationoperation can comprise executing one or more of the instructions addedto compiled program code 306 in step 506 of FIG. 5 (possibly along withusing indicator variables).

In some embodiments, collecting the information about the one or moreruntime conditions comprises collecting information from operations thatuse the pointer and/or operations related to operations that use thepointer that do not directly use the pointer. Recall the examples fromabove, where the tmp pointer was set equal to a pointer to a knownaddress space, and where the tmp pointer was set equal to a secondpointer (the zip) pointer, and the second pointer was set equal to apointer to a known address space. Secondary processing subsystem 106 cancollect the information from these types of operations and/or from otheroperations that indicate an address space for the pointer.

Secondary processing subsystem 106 then performs one or more subsequentoperations using the pointer based on the determined address space (step704). In these embodiments, when performing the one or more subsequentoperations using the pointer, secondary processing subsystem 106 canexecute the additional instructions placed in the program code in step508 of FIG. 5. For example, secondary processing subsystem 106 canreceive a set of possible memory accesses that comprises a separatememory access for each of the one or more address spaces to which thepointer may point and one or more conditional instructions thatdetermine which of the memory access instructions is to be used based onthe outcome of the address space determination for the correspondinginstance of the pointer.

Although embodiments are described that use components (e.g., processingsystem 102, secondary processing subsystem 106, etc.) in electronicdevice 100 for performing both the compilation operation (FIG. 5) andthe execution operation (FIG. 7), the described embodiments are notrequired to use the same electronic device 100 for performing alloperations. Generally, any electronic device that can perform theoperations herein described can be used in either or both of thecompilation and execution operations. For example, in some embodiments,a first electronic device (e.g., a computer system) is used forperforming the compilation operation, and a second, different electronicdevice (e.g., a laptop) is used for performing the execution operation.

The foregoing descriptions of embodiments have been presented only forpurposes of illustration and description. They are not intended to beexhaustive or to limit the embodiments to the forms disclosed.Accordingly, many modifications and variations will be apparent topractitioners skilled in the art. Additionally, the above disclosure isnot intended to limit the embodiments. The scope of the embodiments isdefined by the appended claims.

What is claimed is:
 1. A method for handling a pointer while executingprogram code, comprising: by an electronic device: encountering apointer in the program code that points to an unspecified address space,wherein the pointer includes metadata that indicates one or more addressspaces to which the pointer may point; and determining an address spaceto which the pointer points based on the metadata and one or moreruntime conditions.
 2. The method of claim 1, further comprising:performing one or more subsequent operations using the pointer based onthe determined address space.
 3. The method of claim 2, whereinperforming the one or more subsequent operations using the pointer basedon the determined address space comprises: receiving a set of possiblememory accesses that comprises a separate memory access for each of theone or more address spaces to which the pointer may point; determining amemory access to be performed using the pointer from the set of possiblememory accesses based on the determined address space; and performingthe determined memory access using the pointer.
 4. The method of claim1, wherein determining the address space to which the pointer pointsbased on the metadata and the one or more runtime conditions comprises:collecting information about the one or more runtime conditions, theinformation indicating at least one candidate address space to which thepointer may point; and using the collected information and the metadatato determine an address space to which the pointer points.
 5. The methodof claim 4, wherein collecting the information about the one or moreruntime conditions comprises: collecting information from one of:operations that use the pointer, and operations related to operationsthat use the pointer that do not directly use the pointer.
 6. The methodof claim 1, further comprising: receiving the program code, wherein themetadata that indicates one or more address spaces to which the pointermay point was added during a generation of the program code based atleast in part on an analysis of the program code.
 7. A computer-readablestorage medium containing instructions that, when executed by anelectronic device with computing capabilities, cause the electronicdevice to perform a method for handling a pointer while executingprogram code, the method comprising: encountering a pointer in theprogram code that points to an unspecified address space, wherein thepointer includes metadata that indicates one or more address spaces towhich the pointer may point; and determining an address space to whichthe pointer points based on the metadata and one or more runtimeconditions.
 8. The computer-readable storage medium of claim 7, whereinthe method further comprises: performing one or more subsequentoperations using the pointer based on the determined address space. 9.The computer-readable storage medium of claim 8, wherein performing theone or more subsequent operations using the pointer based on thedetermined address space comprises: receiving a set of possible memoryaccesses that comprises a separate memory access for each of the one ormore address spaces to which the pointer may point; determining a memoryaccess to be performed using the pointer from the set of possible memoryaccesses based on the determined address space; and performing thedetermined memory access using the pointer.
 10. The computer-readablestorage medium of claim 7, wherein determining the address space towhich the pointer points based on the metadata and the one or moreruntime conditions comprises: collecting information about the one ormore runtime conditions, the information indicating at least onecandidate address space to which the pointer may point; and using thecollected information and the metadata to determine an address space towhich the pointer points.
 11. The computer-readable storage medium ofclaim 10, wherein collecting the information about the one or moreruntime conditions comprises: collecting information from one of:operations that use the pointer, and operations related to operationsthat use the pointer that do not directly use the pointer.
 12. Thecomputer-readable storage medium of claim 7, wherein the method furthercomprises: receiving the program code, wherein the metadata thatindicates one or more address spaces to which the pointer may point wasadded during a generation of the program code based at least in part onan analysis of the program code.
 13. An electronic device that handles apointer while executing program code, comprising: at least oneprocessing subsystem; and a memory subsystem; wherein the electronicdevice: encounters a pointer in the program code that points to anunspecified address space, wherein the pointer includes metadata thatindicates one or more address spaces to which the pointer may point; anddetermines an address space to which the pointer points based on themetadata and one or more runtime conditions.
 14. The electronic deviceof claim 13, wherein the electronic device further: performs one or moresubsequent operations using the pointer based on the determined addressspace.
 15. The electronic device of claim 14, wherein performing the oneor more subsequent operations using the pointer based on the determinedaddress space comprises: receiving a set of possible memory accessesthat comprises a separate memory access for each of the one or moreaddress spaces to which the pointer may point; determining a memoryaccess to be performed using the pointer from the set of possible memoryaccesses based on the determined address space; and performing thedetermined memory access using the pointer.
 16. The electronic device ofclaim 13, wherein determining the address space to which the pointerpoints based on the metadata and the one or more runtime conditionscomprises: collecting information about the one or more runtimeconditions, the information indicating at least one candidate addressspace to which the pointer may point; and using the collectedinformation and the metadata to determine an address space to which thepointer points.
 17. The electronic device of claim 16, whereincollecting the information about the one or more runtime conditionscomprises: collecting information from one of: operations that use thepointer, and operations related to operations that use the pointer thatdo not directly use the pointer.
 18. The electronic device of claim 13,wherein the electronic device further: receives the program code,wherein the metadata that indicates one or more address spaces to whichthe pointer may point was added during a generation of the program codebased at least in part on an analysis of the program code.